// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/init/init.h>

static constexpr uint8_t kOneBitmasks[] = {1, 2, 4, 8, 16, 32, 64, 128};
static constexpr uint8_t kZeroBitmasks[] =
    {254, 253, 251, 247, 239, 223, 191, 127};

template <typename index_type, bool Value>
MELON_NOINLINE void setNthBit_array(uint8_t* resultBufferPtr, index_type idx) {
  if constexpr (Value) {
    // Set bit
    resultBufferPtr[idx / 8] |= kOneBitmasks[idx % 8];
  } else {
    // Clear bit
    resultBufferPtr[idx / 8] &= kZeroBitmasks[idx % 8];
  }
}

template <typename index_type, bool Value>
MELON_NOINLINE void setNthBit_shift(uint8_t* resultBufferPtr, index_type idx) {
  if constexpr (Value) {
    // Set bit
    resultBufferPtr[idx / 8] |= (1 << (idx % 8));
  } else {
    // Clear bit
    resultBufferPtr[idx / 8] &= ~(1 << (idx % 8));
  }
}

template <typename index_type, bool Value>
void setNthBit_array_Test(uint32_t iterations, size_t numEntries) {
  melon::BenchmarkSuspender suspender;
  std::vector<uint8_t> vec(numEntries);
  uint8_t* vecPtr = vec.data();
  suspender.dismiss();

  for (index_type i = 0; i < iterations; ++i) {
    setNthBit_array<index_type, Value>(vecPtr, i % numEntries);
  }

  melon::doNotOptimizeAway(vec);
  suspender.rehire();
}

template <typename index_type, bool Value>
void setNthBit_shift_Test(uint32_t iterations, size_t numEntries) {
  melon::BenchmarkSuspender suspender;
  std::vector<uint8_t> vec(numEntries);
  uint8_t* vecPtr = vec.data();
  suspender.dismiss();

  for (index_type i = 0; i < iterations; ++i) {
    setNthBit_shift<index_type, Value>(vecPtr, i % numEntries);
  }

  melon::doNotOptimizeAway(vec);
  suspender.rehire();
}

void BM_setNthBit_array_true_signed(uint32_t iterations, size_t numEntries) {
  setNthBit_array_Test<int32_t, true>(iterations, numEntries);
}

void BM_setNthBit_array_true_unsigned(uint32_t iterations, size_t numEntries) {
  setNthBit_array_Test<uint32_t, true>(iterations, numEntries);
}

void BM_setNthBit_shift_true_signed(uint32_t iterations, size_t numEntries) {
  setNthBit_shift_Test<int32_t, true>(iterations, numEntries);
}

void BM_setNthBit_shift_true_unsigned(uint32_t iterations, size_t numEntries) {
  setNthBit_shift_Test<uint32_t, true>(iterations, numEntries);
}

void BM_setNthBit_array_false_signed(uint32_t iterations, size_t numEntries) {
  setNthBit_array_Test<int32_t, false>(iterations, numEntries);
}

void BM_setNthBit_array_false_unsigned(uint32_t iterations, size_t numEntries) {
  setNthBit_array_Test<uint32_t, false>(iterations, numEntries);
}

void BM_setNthBit_shift_false_signed(uint32_t iterations, size_t numEntries) {
  setNthBit_shift_Test<int32_t, false>(iterations, numEntries);
}

void BM_setNthBit_shift_false_unsigned(uint32_t iterations, size_t numEntries) {
  setNthBit_shift_Test<uint32_t, false>(iterations, numEntries);
}

BENCHMARK_PARAM(BM_setNthBit_shift_true_unsigned, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_array_true_unsigned, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_array_true_signed, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_shift_true_signed, 1000000);
BENCHMARK_DRAW_LINE();

BENCHMARK_PARAM(BM_setNthBit_array_false_unsigned, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_array_false_signed, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_shift_false_signed, 1000000);
BENCHMARK_RELATIVE_PARAM(BM_setNthBit_shift_false_unsigned, 1000000);
BENCHMARK_DRAW_LINE();

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};
  melon::runBenchmarks();
  return 0;
}
